package com.ax.action.system;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.type.IntegerType;
import org.springframework.stereotype.Controller;

import com.ax.pojo.TabConfig;
import com.ax.pojo.TabContents;
import com.ax.pojo.TabDown;
import com.ax.pojo.TabTypes;
import com.ax.service.TabConfigService;
import com.ax.service.TabContentsService;
import com.ax.service.TabDownService;
import com.ax.service.TabTypesService;
import com.ax.utils.BaseAction;
import com.ax.utils.PageInfo;

@Controller("tabContentsAction")
public class TabContentsAction extends BaseAction {
  
	private TabContentsService tabContentsService;
	
	@Resource(name="tabContentsService")
	public void setTabContentsService(TabContentsService tabContentsService) {
		this.tabContentsService = tabContentsService;
	}
	
	private TabTypesService tabTypesService;

	@Resource(name = "tabTypesService")
	public void setTabTypesService(TabTypesService tabTypesService) {
		this.tabTypesService = tabTypesService;
	}

	
	
	private TabConfigService tabConfigService;
	@Resource(name="tabConfigService")
	public void setTabConfigService(TabConfigService tabConfigService) {
		this.tabConfigService = tabConfigService;
	}

	
	private TabDownService tabDownService;
	
	@Resource(name="tabDownService")
	public void setTabDownService(TabDownService tabDownService) {
		this.tabDownService = tabDownService;
	}

	public String findTypes() {
		List<TabTypes> list = tabTypesService.findParentTypes();
		StringBuffer values = new StringBuffer();
		String id=request.getParameter("id");
		List<TabDown> downs=tabDownService.findDowns();
		if (list != null && list.size() > 0) {
			for (TabTypes tabTypes : list) {
				printTypes(values, tabTypes, 0,id);
			}
		}else {
			return "addtypes";
		}
		
		if (downs==null||downs.size()==0) {
		 return "addDownList";
		}
		
		Map<String, TabConfig> map=tabConfigService.findConfig();
		request.setAttribute("types", values.toString());
		request.setAttribute("map",map);
		request.setAttribute("down",downs);
		return "add";
	}

	private void printTypes(StringBuffer values, TabTypes tabTypes, int level,String id) {

		values.append("<option ");
		if (id!=null&&!"".equals(id)) {
			 int i=Integer.parseInt(id);
			 if (i==tabTypes.getTid()) {
				 values.append("selected=\"selected\"");
			}
		}
		if (tabTypes.getTabTypeses().size()>0) {
			 values.append(" style=\"background-color:#cccccc;\" ");
			 values.append("value='" +"-1"+ "'>");
		}else {
			values.append("value='" + tabTypes.getTid() + "'>");
		}
		
		
		for (int i = 0; i < level; i++) {
			values.append("&#12288;");
		}
		if (level > 0) {
			values.append("├");
		}
		values.append(tabTypes.getTname());
		values.append("</option>");
		printType(values, tabTypes, level,id);

	}

	private void printType(StringBuffer values, TabTypes tabTypes, int level,String id) {
		if (tabTypes.getTabTypeses().size() > 0) {
			Set<TabTypes> set = tabTypes.getTabTypeses();
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				TabTypes tp = (TabTypes) iterator.next();
				printTypes(values, tp, level + 1,id);
			}
		}
	}
	
	public String add(){
		boolean flag=true;
		try {
			String oCid=request.getParameter("oCid");
			String oTitle=request.getParameter("oTitle");
			String cpush=request.getParameter("cpush");
			String cauthor=request.getParameter("cauthor");
			String csource=request.getParameter("csource");
			String cviews=request.getParameter("cviews");
			String cdownNum=request.getParameter("cdownNum");
			String ckeywords=request.getParameter("ckeywords");
			String csoftEnvironment=request.getParameter("csoftEnvironment");
			String csoftSize=request.getParameter("csoftSize");
			String csoftLanguage=request.getParameter("csoftLanguage");
			String csoftImpower=request.getParameter("csoftImpower");
			String csoftRank=request.getParameter("csoftRank");
			String csoftPlugin=request.getParameter("csoftPlugin");
			String ccontent=request.getParameter("ccontent");
			String downpath=request.getParameter("downpath");
			TabContents contents=new TabContents();
			contents.setCtitle(oTitle);
			contents.setCpush(cpush);
			contents.setCauthor(cauthor);
			contents.setCsource(csource);
			contents.setCviews(cviews);
			contents.setCdownNum(cdownNum);
			contents.setCkeywords(ckeywords);
			contents.setCsoftEnvironment(csoftEnvironment);
			contents.setCsoftSize(csoftSize);
			contents.setCsoftLanguage(csoftLanguage);
			contents.setCsoftImpower(csoftImpower);
			contents.setCsoftRank(csoftRank);
			contents.setCsoftPlugin(csoftPlugin);
			contents.setCcontent(ccontent);
			contents.setTabTypes(tabTypesService.getParentTypes(oCid));
			contents.setCcreatetime(new Timestamp(new Date().getTime()));
			contents.setCdownPath(downpath);
			tabContentsService.saveContents(contents);
		} catch (Exception e) {
			// TODO: handle exception
			flag=false;
		}
		out.println(flag);
		return null;
	}
	
	
	public String update(){
		boolean flag=true;
		try {
			String sid=request.getParameter("sid");
			String oCid=request.getParameter("oCid");
			String oTitle=request.getParameter("oTitle");
			String cpush=request.getParameter("cpush");
			String cauthor=request.getParameter("cauthor");
			String csource=request.getParameter("csource");
			String cviews=request.getParameter("cviews");
			String cdownNum=request.getParameter("cdownNum");
			String ckeywords=request.getParameter("ckeywords");
			String csoftEnvironment=request.getParameter("csoftEnvironment");
			String csoftSize=request.getParameter("csoftSize");
			String csoftLanguage=request.getParameter("csoftLanguage");
			String csoftImpower=request.getParameter("csoftImpower");
			String csoftRank=request.getParameter("csoftRank");
			String csoftPlugin=request.getParameter("csoftPlugin");
			String ccontent=request.getParameter("ccontent");
			String downpath=request.getParameter("downpath");
			TabContents contents=tabContentsService.getContentsById(Integer.parseInt(sid));
			contents.setCtitle(oTitle);
			contents.setCpush(cpush);
			contents.setCauthor(cauthor);
			contents.setCsource(csource);
			contents.setCviews(cviews);
			contents.setCdownNum(cdownNum);
			contents.setCkeywords(ckeywords);
			contents.setCsoftEnvironment(csoftEnvironment);
			contents.setCsoftSize(csoftSize);
			contents.setCsoftLanguage(csoftLanguage);
			contents.setCsoftImpower(csoftImpower);
			contents.setCsoftRank(csoftRank);
			contents.setCsoftPlugin(csoftPlugin);
			contents.setCcontent(ccontent);
			contents.setTabTypes(tabTypesService.getParentTypes(oCid));
			contents.setCcreatetime(new Timestamp(new Date().getTime()));
			contents.setCdownPath(downpath);
			tabContentsService.updateContents(contents);
		} catch (Exception e) {
			// TODO: handle exception
			flag=false;
		}
		out.println(flag);
		return null;
	}
	
	private PageInfo page;
	public PageInfo getPage() {
		return page;
	}

	public void setPage(PageInfo page) {
		this.page = page;
	}

	public String find(){
		if(page==null){
			page=new PageInfo();
		}
	  page=tabContentsService.findPageContents(page.getCurPage(), "");
		return "lists";
	}
	
	public String get(){
		try {
			List<TabTypes> list = tabTypesService.findParentTypes();
			Integer id=Integer.parseInt(request.getParameter("id"));
		  TabContents tabContents=tabContentsService.getContentsById(id);
			StringBuffer values=new StringBuffer();
		  	for (TabTypes tabTypes : list) {
		  		printTypes(values, tabTypes, 0,tabContents.getTabTypes().getTid()+"");
			}
		  	request.setAttribute("types",values.toString());
		  	request.setAttribute("map",tabConfigService.findConfig());
		  	request.setAttribute("down", tabDownService.findDowns());
		  	request.setAttribute("tabContents", tabContents);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return "get";
	}
	
	public String del(){
		try {
			Integer id=Integer.parseInt(request.getParameter("id"));
			TabContents tabContents=tabContentsService.getContentsById(id);
			tabContentsService.delContents(tabContents);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "find";
	}
}
